<!DOCTYPE html>
<html lang='zh-CN'>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>用户管理</title>
<link rel="icon" href="favicon.ico" type="image/ico">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/materialdesignicons.min.css" rel="stylesheet">
<link href="css/animate.css" rel="stylesheet">
<link href="css/style.min.css" rel="stylesheet">
<link href="js/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid p-t-15">

  <div class="row">
    <div class="col-lg-12">
      <div class="card">
        <div class="card-header">

          <div id="toolbar" class="toolbar-btn-action">
              <form class="form-inline" method="post" id="searchForm">
                <div class="form-group">
                  <label>用户名称:</label>
                  <input type="text" class="form-control" name="username">
                </div>
                <div class="form-group">
                  <label>用户账号:</label>
                  <input type="text" class="form-control" name="account">
                </div>
                <div class="form-group">
                  <button class="btn btn-info btn_query btn-round" type="button" id="searchFormBtn">查询</button>
                </div>
                <@shiro.hasPermission name="system:user:add">
                <div class="form-group">
                  <button class="btn btn-success btn_query btn-round" type="button" id="addBtn">添加</button>
                </div>
              </@shiro.hasPermission>
                <@shiro.hasPermission name="system:user:edit">
                <div class="form-group">
                  <button class="btn btn-warning btn_query btn-round" type="button" id="editBtn">编辑</button>
                </div>
              </@shiro.hasPermission>
                <@shiro.hasPermission name="system:user:disable">
                <div class="form-group">
                  <button class="btn btn-danger btn_query btn-round" type="button" onclick="changeUserStatus(0)">禁用</button>
                </div>
               </@shiro.hasPermission>
                <@shiro.hasPermission name="system:user:open">
                <div class="form-group">
                  <button class="btn btn-purple btn_query btn-round" type="button" onclick="changeUserStatus(1)">启用</button>
                </div>
                </@shiro.hasPermission>
              </form>
          </div>

        </div>
        <div class="card-body">

          <table id="grid_data" class="table text-nowrap"></table>

        </div>
      </div>
    </div>
  </div>

  <!--  添加与编辑模态框 -->
  <div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog modal-lg" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <div id="error_tips" style="color: #c9302c"></div>
        </div>
        <div class="modal-body">
          <form id="addOrEditForm" action="/addOrEditUser" method="post">
            <div class="row">
              <div class="col-xs-6">
                <div class="input-group m-b-10">
                  <span class="input-group-addon" id="basic-account">用户账户</span>
                  <input type="hidden" name="id">
                  <input type="text" name="account" class="form-control" aria-describedby="basic-account" placeholder="用户账号不能为空" id="account" maxlength="11">
                  <div class="help-block with-errors"></div>
                </div>
              </div>
              <div class="col-xs-6">
                <div class="input-group m-b-10">
                  <span class="input-group-addon" id="basic-username">用户名称</span>
                  <input type="text" name="username" class="form-control" aria-describedby="basic-username" placeholder="用户名称不能为空" id="username">
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-xs-6">
                <div class="input-group m-b-10">
                  <span class="input-group-addon" id="basic-dept">所属部门</span>
                  <input type="hidden" name="deptId" class="form-control" aria-describedby="basic-dept" id="deptId">
                  <input type="text" name="deptName" class="form-control" aria-describedby="basic-dept" id="baidu">
                  <div class="input-group-btn">
                    <ul class="dropdown-menu dropdown-menu-right" role="menu">
                    </ul>
                  </div>
                </div>
              </div>
              <div class="col-xs-6">
                <div class="input-group m-b-10">
                  <span class="input-group-addon" id="basic-email">用户邮箱</span>
                  <input type="text" id="email" name="email" class="form-control" aria-describedby="basic-email">
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-xs-12">
                <div class="input-group m-b-10">
                  <span class="input-group-addon" id="basic-profile">用户简介</span>
                  <input type="text" name="profile" class="form-control" aria-describedby="basic-profile">
                </div>
              </div>
            </div>

            <div class="form-group" id="role">
              <div>
                <label for="role" class="control-label" style="margin-left: 60px">用户角色：</label>
              </div>
              <div class="row" style="margin-top: 10px">
                <div class="col-sm-4 col-sm-offset-1">
                  <select multiple class="form-control allRoles" size="12">
                  </select>
                </div>
                <div class="col-sm-2" style="margin-top: 16px;" align="center">
                  <div>
                    <a type="button" class="btn btn-info  " style="margin-top: 10px"
                       onclick="moveSelected('allRoles', 'selfRoles', 1)">&nbsp;&gt;&nbsp;</a>
                    <br>
                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                       onclick="moveSelected('selfRoles', 'allRoles', 2)">&nbsp;&lt;&nbsp;</a>
                    <br>
                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                       onclick="moveAll('allRoles', 'selfRoles', 1)">&gt;&gt;</a>
                    <br>
                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                       onclick="moveAll('selfRoles', 'allRoles', 2)">&lt;&lt;</a>
                  </div>
                </div>

                <div class="col-sm-4">
                  <select multiple class="form-control selfRoles" size="12" name="roleCodes">
                  </select>
                </div>
              </div>
            </div>
          </form>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-primary addSaveBtn">保存</button>
          </div>
        </div>

      </div>
    </div>
  </div>


  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script type="text/javascript" src="js/bootstrap.min.js"></script>
  <script type="text/javascript" src="js/bootstrap-table/bootstrap-table.js"></script>
  <script type="text/javascript" src="js/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
  <script type="text/javascript" src="js/zero2one/zero2one.version.js"></script>
  <script type="text/javascript" src="js/zero2one/zero2one.core.js"></script>
  <script type="text/javascript" src="js/zero2one/zero2one.message.js"></script>
  <!-- bootstrap表单验证插件 -->
  <script type="text/javascript" src="js/bootstrap-validator-master/dist/validator.js"></script>
  <!-- jquery-form表单提交插件 -->
  <script type="text/javascript" src="js/jquery-form/jquery.form.js"></script>
  <!-- bootstrap弹出框插件 -->
  <script type="text/javascript" src="js/jquery.bootstrap.min.js"></script>
  <!-- 表单填充插件 -->
  <script type="text/javascript" src="js/jquery-form/jquery.formloader.js"></script>
  <!-- 邮箱自动填充插件 -->
  <script type="text/javascript" src="js/email/autoMail.js"></script>
  <!-- boostrap-combox下拉框检索插件 -->
  <script type="text/javascript" src="js/suggest/bootstrap-suggest.min.js"></script>
  <script type="text/javascript" src="js/zero2one/zero2one.suggest.js"></script>

  <script type="text/javascript">

      //点击添加按钮弹出模态框
      $("#addBtn").click(function () {
          $("#addOrEditForm")[0].reset();
          $("#account").prop("readonly", false);
          $(".allRoles,.selfRoles").empty();

          $.post("/system/role/getAllRole", {"f":new Date().getTime()},
              function(data){
                  //第一个参数：数组
                  $.each(data.data,function (index,value) {
                      $(".allRoles").append("<option value='"+value.role_code+"'>"+value.role_name+"</option>");
                  });
              }, "json");

          $("#addOrEditModal").modal("show");
      });

      //邮箱自动匹配
      $('#email').autoMail(
          {emails: ['qq.com', '163.com', '126.com', 'sina.com', 'sohu.com', 'yahoo.cn', 'gmail.com', 'hotmail.com', 'live.cn']
       });

      //根据部门名称匹配部门数据
      zero2one.suggest("baidu","/getDeptByName?keyword=","deptId","baidu","部门");

      function moveSelected(target, des, type) {
          $("."+target+" option:selected").prop("selected",type == 1 ? true : false).appendTo($("."+des));
      }

      function moveAll(target, des, type) {
          $("."+target+" option").prop("selected",type == 1 ? true : false).appendTo($("."+des));
      }

      //添加点击保存按钮
      $(".addSaveBtn").click(function () {

          if($.trim($("#account").val()).length == 0){
              $("#error_tips").html("用户账号不能为空");
              return;
          }
          if($.trim($("#username").val()).length == 0){
              $("#error_tips").html("用户名称不能为空");
              return;
          }

          $(".selfRoles option").prop("selected",true);

          $("#addOrEditForm").ajaxSubmit(function (data) {
              zero2one.msg(data);
          });

      });

      //设置表头
      var columns = [
          {checkbox: true},
          {field: 'id',title: 'ID'},
          {field: 'account',title: '用户账号'},
          {field: 'username',title: '用户名称'},
          {field: 'email',title: '用户邮箱'},
          {field: 'username',title: '创建人'},
          {field: 'profile',title: '用户简介'},
          {field: 'user_status',title: '用户状态'},
          {field: 'deptId',title: '部门id',visible: false},
          {field: 'deptName',title: '所属部门'},
          {field: 'role_name',title: '所属角色'},
          {field: 'create_time',title: '创建时间'}
      ];

      //进入主界面加载数据
      zero2one.datagrid("grid_data", "/userPage",function (params) {
          return $.extend(zero2one.tableInitParam(params), zero2one.form2json("searchForm"));
      }, columns);

      //点击查询按钮重新加载页面数据
      $("#searchFormBtn").click(function () {
          $('#grid_data').bootstrapTable('refresh');
      });

      //改变用户状态
      function changeUserStatus(status) {
          //获取选中行的数据
          var datas = $('#grid_data').bootstrapTable('getSelections');
          //判断是否选中行数据，如果没有则提示
          if(datas.length > 0){
              $.messager.confirm("温馨提示", "您确定要更改用户状态吗?", function () {
                  //发送请求,执行批量删除
                  $.post("/changeUserStatus", {"status" : status,"ids" : zero2one.array2str(datas)},
                      function(data){
                          if (data.success) {
                              window.location.reload();
                          } else {
                              $.messager.alert("温馨提示", data.msg)
                          }
                      }, "json");
              })
          }else{
              $.messager.alert("温馨提示", "请至少选择一条数据操作");

          }
      }

      //编辑用户
      $("#editBtn").click(function () {
          $(".allRoles,.selfRoles").empty();
          //获取选中行的数据
          var datas = $('#grid_data').bootstrapTable('getSelections');
          //判断是否选中行数据，如果没有则提示
          if(datas.length == 1){

              $("#account").prop("readonly", true);

              //填充表单数据
              $("#addOrEditForm").formloader({data: datas[0]});

              $.post("/getUserAllRole", {"userId" : datas[0].id},
                  function(data){
                        $.each(data.data, function (i, v) {
                            if(v.type_id == 1){
                                $(".allRoles").append("<option value='"+v.role_code+"'>"+v.role_name+"</option>");
                            }else{
                                $(".selfRoles").append("<option value='"+v.role_code+"'>"+v.role_name+"</option>");
                            }
                        });
                  }, "json");

              //打开模态框
              $("#addOrEditModal").modal("show");
          }else{
              $.messager.alert("温馨提示", "请选择一条数据操作");
          }

      });

  </script>

</div>
</body>
</html>
